﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


/*   Google Data API Namespaces    */

using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;

namespace DomainLogicLayer
{

    public class ContactEntryDataGateway
    {

        public static string GetUniformPhoneNumber(string contactPhoneNumber)
        {
            string uniformPhoneNumber = "";

            char[] characterArray = contactPhoneNumber.ToCharArray();

            foreach (char character in characterArray)
            {
                if (Char.IsDigit(character))
                {
                    uniformPhoneNumber += character;
                }
            }

            return uniformPhoneNumber;
        }

        public static ContactEntry FindByEmail(string contactPhoneNumber)
        {
            ContactEntry contactEntry = null;

            ContactsFeed feed = GetContactsFeed();

            foreach (ContactEntry entry in feed.Entries)
            {
                foreach (EMail email in entry.Emails)
                {
                    if (email.Address == contactPhoneNumber)
                    {
                        contactEntry = entry;
                        break;
                    }

                }

            }


            return contactEntry;
        }

        public static ContactEntry FindByPhoneNumber(string contactPhoneNumber)
        {
            ContactEntry contactEntry = null;

            ContactsFeed feed = GetContactsFeed();

            foreach (ContactEntry entry in feed.Entries)
            {
                foreach (PhoneNumber phoneNumber in entry.Phonenumbers)
                {
                    if (GetUniformPhoneNumber(phoneNumber.Value) == GetUniformPhoneNumber(contactPhoneNumber))
                    {
                        contactEntry = entry;
                        break;
                    }

                }

            }


            return contactEntry;
        }

        private static ContactsFeed GetContactsFeed()
        {
            ContactsService service = new ContactsService("cp");
            service.setUserCredentials("username@gmail.com", "password");
            string uri = "http://www.google.com/m8/feeds/contacts/username@gmail.com/full";
            ContactsQuery query = new ContactsQuery(uri);
            ContactsFeed feed = service.Query(query);

            return feed;
        }

        public static void Main(string[] args)
        {
            ContactEntry contactEntry = FindByEmail("username@gmail.com");
            Display(contactEntry);

            contactEntry = FindByPhoneNumber("+1 (514) 123-4567");
            Display(contactEntry);
            //-----------------------------------------------------------------




        }

        public static void Display(ContactEntry contactEntry)
        {
            if (contactEntry != null)
            {
                Console.WriteLine("Name: " + contactEntry.Title.Text);
                Console.WriteLine("\nEmails:");
                if (contactEntry.Emails.Count == 0)
                {
                    Console.WriteLine("\tNo Emails Added.");
                }
                else
                {
                    foreach (EMail email in contactEntry.Emails)
                    {
                        Console.WriteLine("\t" + email.Address);
                    }
                }

                Console.WriteLine("\nPhones:");
                if (contactEntry.Phonenumbers.Count == 0)
                {
                    Console.WriteLine("\tNo Phone Numbers Added.");
                }
                else
                {
                    foreach (PhoneNumber phoneNumber in contactEntry.Phonenumbers)
                    {
                        Console.WriteLine("\t" + phoneNumber.Value);
                    }
                }
            }

        }

    }



}
